<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>xhrio.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_net_xhrio.js.html">xhrio.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Wrapper class for handling XmlHttpRequests.
<a name="line17"></a> *
<a name="line18"></a> * One off requests can be sent through goog.net.XhrIo.send() or an
<a name="line19"></a> * instance can be created to send multiple requests.  Each request uses its
<a name="line20"></a> * own XmlHttpRequest object and handles clearing of the event callback to
<a name="line21"></a> * ensure no leaks.
<a name="line22"></a> *
<a name="line23"></a> * XhrIo is event based, it dispatches events when a request finishes, fails or
<a name="line24"></a> * succeeds or when the ready-state changes. The ready-state or timeout event
<a name="line25"></a> * fires first, followed by a generic completed event. Then the abort, error,
<a name="line26"></a> * or success event is fired as appropriate. Lastly, the ready event will fire
<a name="line27"></a> * to indicate that the object may be used to make another request.
<a name="line28"></a> *
<a name="line29"></a> * The error event may also be called before completed and
<a name="line30"></a> * ready-state-change if the XmlHttpRequest.open() or .send() methods throw.
<a name="line31"></a> *
<a name="line32"></a> * This class does not support multiple requests, queuing, or prioritization.
<a name="line33"></a> *
<a name="line34"></a> * Tested = IE6, FF1.5, Safari, Opera 8.5
<a name="line35"></a> *
<a name="line36"></a> * TODO(user): Error cases aren&#39;t playing nicely in Safari.
<a name="line37"></a> *
<a name="line38"></a> */
<a name="line39"></a>
<a name="line40"></a>
<a name="line41"></a>goog.provide(&#39;goog.net.XhrIo&#39;);
<a name="line42"></a>goog.provide(&#39;goog.net.XhrIo.ResponseType&#39;);
<a name="line43"></a>
<a name="line44"></a>goog.require(&#39;goog.Timer&#39;);
<a name="line45"></a>goog.require(&#39;goog.array&#39;);
<a name="line46"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line47"></a>goog.require(&#39;goog.debug.entryPointRegistry&#39;);
<a name="line48"></a>goog.require(&#39;goog.events&#39;);
<a name="line49"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line50"></a>goog.require(&#39;goog.json&#39;);
<a name="line51"></a>goog.require(&#39;goog.net.ErrorCode&#39;);
<a name="line52"></a>goog.require(&#39;goog.net.EventType&#39;);
<a name="line53"></a>goog.require(&#39;goog.net.HttpStatus&#39;);
<a name="line54"></a>goog.require(&#39;goog.net.XmlHttp&#39;);
<a name="line55"></a>goog.require(&#39;goog.object&#39;);
<a name="line56"></a>goog.require(&#39;goog.structs&#39;);
<a name="line57"></a>goog.require(&#39;goog.structs.Map&#39;);
<a name="line58"></a>goog.require(&#39;goog.uri.utils&#39;);
<a name="line59"></a>
<a name="line60"></a>
<a name="line61"></a>
<a name="line62"></a>/**
<a name="line63"></a> * Basic class for handling XMLHttpRequests.
<a name="line64"></a> * @param {goog.net.XmlHttpFactory=} opt_xmlHttpFactory Factory to use when
<a name="line65"></a> *     creating XMLHttpRequest objects.
<a name="line66"></a> * @constructor
<a name="line67"></a> * @extends {goog.events.EventTarget}
<a name="line68"></a> */
<a name="line69"></a>goog.net.XhrIo = function(opt_xmlHttpFactory) {
<a name="line70"></a>  goog.events.EventTarget.call(this);
<a name="line71"></a>
<a name="line72"></a>  /**
<a name="line73"></a>   * Map of default headers to add to every request, use:
<a name="line74"></a>   * XhrIo.headers.set(name, value)
<a name="line75"></a>   * @type {goog.structs.Map}
<a name="line76"></a>   */
<a name="line77"></a>  this.headers = new goog.structs.Map();
<a name="line78"></a>
<a name="line79"></a>  /**
<a name="line80"></a>   * Optional XmlHttpFactory
<a name="line81"></a>   * @type {goog.net.XmlHttpFactory}
<a name="line82"></a>   * @private
<a name="line83"></a>   */
<a name="line84"></a>  this.xmlHttpFactory_ = opt_xmlHttpFactory || null;
<a name="line85"></a>};
<a name="line86"></a>goog.inherits(goog.net.XhrIo, goog.events.EventTarget);
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>/**
<a name="line90"></a> * Response types that may be requested for XMLHttpRequests.
<a name="line91"></a> * @enum {string}
<a name="line92"></a> * @see http://www.w3.org/TR/XMLHttpRequest/#the-responsetype-attribute
<a name="line93"></a> */
<a name="line94"></a>goog.net.XhrIo.ResponseType = {
<a name="line95"></a>  DEFAULT: &#39;&#39;,
<a name="line96"></a>  TEXT: &#39;text&#39;,
<a name="line97"></a>  DOCUMENT: &#39;document&#39;,
<a name="line98"></a>  // Not supported as of Chrome 10.0.612.1 dev
<a name="line99"></a>  BLOB: &#39;blob&#39;,
<a name="line100"></a>  ARRAY_BUFFER: &#39;arraybuffer&#39;
<a name="line101"></a>};
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>/**
<a name="line105"></a> * A reference to the XhrIo logger
<a name="line106"></a> * @type {goog.debug.Logger}
<a name="line107"></a> * @private
<a name="line108"></a> */
<a name="line109"></a>goog.net.XhrIo.prototype.logger_ =
<a name="line110"></a>    goog.debug.Logger.getLogger(&#39;goog.net.XhrIo&#39;);
<a name="line111"></a>
<a name="line112"></a>
<a name="line113"></a>/**
<a name="line114"></a> * The Content-Type HTTP header name
<a name="line115"></a> * @type {string}
<a name="line116"></a> */
<a name="line117"></a>goog.net.XhrIo.CONTENT_TYPE_HEADER = &#39;Content-Type&#39;;
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/**
<a name="line121"></a> * The pattern matching the &#39;http&#39; and &#39;https&#39; URI schemes
<a name="line122"></a> * @type {!RegExp}
<a name="line123"></a> */
<a name="line124"></a>goog.net.XhrIo.HTTP_SCHEME_PATTERN = /^https?$/i;
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * The Content-Type HTTP header value for a url-encoded form
<a name="line129"></a> * @type {string}
<a name="line130"></a> */
<a name="line131"></a>goog.net.XhrIo.FORM_CONTENT_TYPE =
<a name="line132"></a>    &#39;application/x-www-form-urlencoded;charset=utf-8&#39;;
<a name="line133"></a>
<a name="line134"></a>
<a name="line135"></a>/**
<a name="line136"></a> * All non-disposed instances of goog.net.XhrIo created
<a name="line137"></a> * by {@link goog.net.XhrIo.send} are in this Array.
<a name="line138"></a> * @see goog.net.XhrIo.cleanup
<a name="line139"></a> * @type {Array.&lt;goog.net.XhrIo&gt;}
<a name="line140"></a> * @private
<a name="line141"></a> */
<a name="line142"></a>goog.net.XhrIo.sendInstances_ = [];
<a name="line143"></a>
<a name="line144"></a>
<a name="line145"></a>/**
<a name="line146"></a> * Static send that creates a short lived instance of XhrIo to send the
<a name="line147"></a> * request.
<a name="line148"></a> * @see goog.net.XhrIo.cleanup
<a name="line149"></a> * @param {string|goog.Uri} url Uri to make request to.
<a name="line150"></a> * @param {Function=} opt_callback Callback function for when request is
<a name="line151"></a> *     complete.
<a name="line152"></a> * @param {string=} opt_method Send method, default: GET.
<a name="line153"></a> * @param {ArrayBuffer|Blob|Document|FormData|GearsBlob|string=} opt_content
<a name="line154"></a> *     Post data. This can be a Gears blob if the underlying HTTP request object
<a name="line155"></a> *     is a Gears HTTP request.
<a name="line156"></a> * @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the
<a name="line157"></a> *     request.
<a name="line158"></a> * @param {number=} opt_timeoutInterval Number of milliseconds after which an
<a name="line159"></a> *     incomplete request will be aborted; 0 means no timeout is set.
<a name="line160"></a> * @param {boolean=} opt_withCredentials Whether to send credentials with the
<a name="line161"></a> *     request. Default to false. See {@link goog.net.XhrIo#setWithCredentials}.
<a name="line162"></a> */
<a name="line163"></a>goog.net.XhrIo.send = function(url, opt_callback, opt_method, opt_content,
<a name="line164"></a>                               opt_headers, opt_timeoutInterval,
<a name="line165"></a>                               opt_withCredentials) {
<a name="line166"></a>  var x = new goog.net.XhrIo();
<a name="line167"></a>  goog.net.XhrIo.sendInstances_.push(x);
<a name="line168"></a>  if (opt_callback) {
<a name="line169"></a>    goog.events.listen(x, goog.net.EventType.COMPLETE, opt_callback);
<a name="line170"></a>  }
<a name="line171"></a>  goog.events.listen(x,
<a name="line172"></a>                     goog.net.EventType.READY,
<a name="line173"></a>                     goog.partial(goog.net.XhrIo.cleanupSend_, x));
<a name="line174"></a>  if (opt_timeoutInterval) {
<a name="line175"></a>    x.setTimeoutInterval(opt_timeoutInterval);
<a name="line176"></a>  }
<a name="line177"></a>  if (opt_withCredentials) {
<a name="line178"></a>    x.setWithCredentials(opt_withCredentials);
<a name="line179"></a>  }
<a name="line180"></a>  x.send(url, opt_method, opt_content, opt_headers);
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * Disposes all non-disposed instances of goog.net.XhrIo created by
<a name="line186"></a> * {@link goog.net.XhrIo.send}.
<a name="line187"></a> * {@link goog.net.XhrIo.send} cleans up the goog.net.XhrIo instance
<a name="line188"></a> * it creates when the request completes or fails.  However, if
<a name="line189"></a> * the request never completes, then the goog.net.XhrIo is not disposed.
<a name="line190"></a> * This can occur if the window is unloaded before the request completes.
<a name="line191"></a> * We could have {@link goog.net.XhrIo.send} return the goog.net.XhrIo
<a name="line192"></a> * it creates and make the client of {@link goog.net.XhrIo.send} be
<a name="line193"></a> * responsible for disposing it in this case.  However, this makes things
<a name="line194"></a> * significantly more complicated for the client, and the whole point
<a name="line195"></a> * of {@link goog.net.XhrIo.send} is that it&#39;s simple and easy to use.
<a name="line196"></a> * Clients of {@link goog.net.XhrIo.send} should call
<a name="line197"></a> * {@link goog.net.XhrIo.cleanup} when doing final
<a name="line198"></a> * cleanup on window unload.
<a name="line199"></a> */
<a name="line200"></a>goog.net.XhrIo.cleanup = function() {
<a name="line201"></a>  var instances = goog.net.XhrIo.sendInstances_;
<a name="line202"></a>  while (instances.length) {
<a name="line203"></a>    instances.pop().dispose();
<a name="line204"></a>  }
<a name="line205"></a>};
<a name="line206"></a>
<a name="line207"></a>
<a name="line208"></a>/**
<a name="line209"></a> * Installs exception protection for all entry point introduced by
<a name="line210"></a> * goog.net.XhrIo instances which are not protected by
<a name="line211"></a> * {@link goog.debug.ErrorHandler#protectWindowSetTimeout},
<a name="line212"></a> * {@link goog.debug.ErrorHandler#protectWindowSetInterval}, or
<a name="line213"></a> * {@link goog.events.protectBrowserEventEntryPoint}.
<a name="line214"></a> *
<a name="line215"></a> * @param {goog.debug.ErrorHandler} errorHandler Error handler with which to
<a name="line216"></a> *     protect the entry point(s).
<a name="line217"></a> */
<a name="line218"></a>goog.net.XhrIo.protectEntryPoints = function(errorHandler) {
<a name="line219"></a>  goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ =
<a name="line220"></a>      errorHandler.protectEntryPoint(
<a name="line221"></a>          goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_);
<a name="line222"></a>};
<a name="line223"></a>
<a name="line224"></a>
<a name="line225"></a>/**
<a name="line226"></a> * Disposes of the specified goog.net.XhrIo created by
<a name="line227"></a> * {@link goog.net.XhrIo.send} and removes it from
<a name="line228"></a> * {@link goog.net.XhrIo.pendingStaticSendInstances_}.
<a name="line229"></a> * @param {goog.net.XhrIo} XhrIo An XhrIo created by
<a name="line230"></a> *     {@link goog.net.XhrIo.send}.
<a name="line231"></a> * @private
<a name="line232"></a> */
<a name="line233"></a>goog.net.XhrIo.cleanupSend_ = function(XhrIo) {
<a name="line234"></a>  XhrIo.dispose();
<a name="line235"></a>  goog.array.remove(goog.net.XhrIo.sendInstances_, XhrIo);
<a name="line236"></a>};
<a name="line237"></a>
<a name="line238"></a>
<a name="line239"></a>/**
<a name="line240"></a> * Whether XMLHttpRequest is active.  A request is active from the time send()
<a name="line241"></a> * is called until onReadyStateChange() is complete, or error() or abort()
<a name="line242"></a> * is called.
<a name="line243"></a> * @type {boolean}
<a name="line244"></a> * @private
<a name="line245"></a> */
<a name="line246"></a>goog.net.XhrIo.prototype.active_ = false;
<a name="line247"></a>
<a name="line248"></a>
<a name="line249"></a>/**
<a name="line250"></a> * Reference to an XMLHttpRequest object that is being used for the transfer.
<a name="line251"></a> * @type {XMLHttpRequest|GearsHttpRequest}
<a name="line252"></a> * @private
<a name="line253"></a> */
<a name="line254"></a>goog.net.XhrIo.prototype.xhr_ = null;
<a name="line255"></a>
<a name="line256"></a>
<a name="line257"></a>/**
<a name="line258"></a> * The options to use with the current XMLHttpRequest object.
<a name="line259"></a> * @type {Object}
<a name="line260"></a> * @private
<a name="line261"></a> */
<a name="line262"></a>goog.net.XhrIo.prototype.xhrOptions_ = null;
<a name="line263"></a>
<a name="line264"></a>
<a name="line265"></a>/**
<a name="line266"></a> * Last URL that was requested.
<a name="line267"></a> * @type {string|goog.Uri}
<a name="line268"></a> * @private
<a name="line269"></a> */
<a name="line270"></a>goog.net.XhrIo.prototype.lastUri_ = &#39;&#39;;
<a name="line271"></a>
<a name="line272"></a>
<a name="line273"></a>/**
<a name="line274"></a> * Method for the last request.
<a name="line275"></a> * @type {string}
<a name="line276"></a> * @private
<a name="line277"></a> */
<a name="line278"></a>goog.net.XhrIo.prototype.lastMethod_ = &#39;&#39;;
<a name="line279"></a>
<a name="line280"></a>
<a name="line281"></a>/**
<a name="line282"></a> * Last error code.
<a name="line283"></a> * @type {goog.net.ErrorCode}
<a name="line284"></a> * @private
<a name="line285"></a> */
<a name="line286"></a>goog.net.XhrIo.prototype.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
<a name="line287"></a>
<a name="line288"></a>
<a name="line289"></a>/**
<a name="line290"></a> * Last error message.
<a name="line291"></a> * @type {Error|string}
<a name="line292"></a> * @private
<a name="line293"></a> */
<a name="line294"></a>goog.net.XhrIo.prototype.lastError_ = &#39;&#39;;
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>/**
<a name="line298"></a> * This is used to ensure that we don&#39;t dispatch an multiple ERROR events. This
<a name="line299"></a> * can happen in IE when it does a synchronous load and one error is handled in
<a name="line300"></a> * the ready statte change and one is handled due to send() throwing an
<a name="line301"></a> * exception.
<a name="line302"></a> * @type {boolean}
<a name="line303"></a> * @private
<a name="line304"></a> */
<a name="line305"></a>goog.net.XhrIo.prototype.errorDispatched_ = false;
<a name="line306"></a>
<a name="line307"></a>
<a name="line308"></a>/**
<a name="line309"></a> * Used to make sure we don&#39;t fire the complete event from inside a send call.
<a name="line310"></a> * @type {boolean}
<a name="line311"></a> * @private
<a name="line312"></a> */
<a name="line313"></a>goog.net.XhrIo.prototype.inSend_ = false;
<a name="line314"></a>
<a name="line315"></a>
<a name="line316"></a>/**
<a name="line317"></a> * Used in determining if a call to {@link #onReadyStateChange_} is from within
<a name="line318"></a> * a call to this.xhr_.open.
<a name="line319"></a> * @type {boolean}
<a name="line320"></a> * @private
<a name="line321"></a> */
<a name="line322"></a>goog.net.XhrIo.prototype.inOpen_ = false;
<a name="line323"></a>
<a name="line324"></a>
<a name="line325"></a>/**
<a name="line326"></a> * Used in determining if a call to {@link #onReadyStateChange_} is from within
<a name="line327"></a> * a call to this.xhr_.abort.
<a name="line328"></a> * @type {boolean}
<a name="line329"></a> * @private
<a name="line330"></a> */
<a name="line331"></a>goog.net.XhrIo.prototype.inAbort_ = false;
<a name="line332"></a>
<a name="line333"></a>
<a name="line334"></a>/**
<a name="line335"></a> * Number of milliseconds after which an incomplete request will be aborted and
<a name="line336"></a> * a {@link goog.net.EventType.TIMEOUT} event raised; 0 means no timeout is set.
<a name="line337"></a> * @type {number}
<a name="line338"></a> * @private
<a name="line339"></a> */
<a name="line340"></a>goog.net.XhrIo.prototype.timeoutInterval_ = 0;
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>/**
<a name="line344"></a> * Window timeout ID used to cancel the timeout event handler if the request
<a name="line345"></a> * completes successfully.
<a name="line346"></a> * @type {Object}
<a name="line347"></a> * @private
<a name="line348"></a> */
<a name="line349"></a>goog.net.XhrIo.prototype.timeoutId_ = null;
<a name="line350"></a>
<a name="line351"></a>
<a name="line352"></a>/**
<a name="line353"></a> * The requested type for the response. The empty string means use the default
<a name="line354"></a> * XHR behavior.
<a name="line355"></a> * @type {goog.net.XhrIo.ResponseType}
<a name="line356"></a> * @private
<a name="line357"></a> */
<a name="line358"></a>goog.net.XhrIo.prototype.responseType_ = goog.net.XhrIo.ResponseType.DEFAULT;
<a name="line359"></a>
<a name="line360"></a>
<a name="line361"></a>/**
<a name="line362"></a> * Whether a &quot;credentialed&quot; request is to be sent (one that is aware of cookies
<a name="line363"></a> * and authentication) . This is applicable only for cross-domain requests and
<a name="line364"></a> * more recent browsers that support this part of the HTTP Access Control
<a name="line365"></a> * standard.
<a name="line366"></a> *
<a name="line367"></a> * @see http://www.w3.org/TR/XMLHttpRequest/#the-withcredentials-attribute
<a name="line368"></a> *
<a name="line369"></a> * @type {boolean}
<a name="line370"></a> * @private
<a name="line371"></a> */
<a name="line372"></a>goog.net.XhrIo.prototype.withCredentials_ = false;
<a name="line373"></a>
<a name="line374"></a>
<a name="line375"></a>/**
<a name="line376"></a> * Returns the number of milliseconds after which an incomplete request will be
<a name="line377"></a> * aborted, or 0 if no timeout is set.
<a name="line378"></a> * @return {number} Timeout interval in milliseconds.
<a name="line379"></a> */
<a name="line380"></a>goog.net.XhrIo.prototype.getTimeoutInterval = function() {
<a name="line381"></a>  return this.timeoutInterval_;
<a name="line382"></a>};
<a name="line383"></a>
<a name="line384"></a>
<a name="line385"></a>/**
<a name="line386"></a> * Sets the number of milliseconds after which an incomplete request will be
<a name="line387"></a> * aborted and a {@link goog.net.EventType.TIMEOUT} event raised; 0 means no
<a name="line388"></a> * timeout is set.
<a name="line389"></a> * @param {number} ms Timeout interval in milliseconds; 0 means none.
<a name="line390"></a> */
<a name="line391"></a>goog.net.XhrIo.prototype.setTimeoutInterval = function(ms) {
<a name="line392"></a>  this.timeoutInterval_ = Math.max(0, ms);
<a name="line393"></a>};
<a name="line394"></a>
<a name="line395"></a>
<a name="line396"></a>/**
<a name="line397"></a> * Sets the desired type for the response. At time of writing, this is only
<a name="line398"></a> * supported in very recent versions of WebKit (10.0.612.1 dev and later).
<a name="line399"></a> *
<a name="line400"></a> * If this is used, the response may only be accessed via {@link #getResponse}.
<a name="line401"></a> *
<a name="line402"></a> * @param {goog.net.XhrIo.ResponseType} type The desired type for the response.
<a name="line403"></a> */
<a name="line404"></a>goog.net.XhrIo.prototype.setResponseType = function(type) {
<a name="line405"></a>  this.responseType_ = type;
<a name="line406"></a>};
<a name="line407"></a>
<a name="line408"></a>
<a name="line409"></a>/**
<a name="line410"></a> * Gets the desired type for the response.
<a name="line411"></a> * @return {goog.net.XhrIo.ResponseType} The desired type for the response.
<a name="line412"></a> */
<a name="line413"></a>goog.net.XhrIo.prototype.getResponseType = function() {
<a name="line414"></a>  return this.responseType_;
<a name="line415"></a>};
<a name="line416"></a>
<a name="line417"></a>
<a name="line418"></a>/**
<a name="line419"></a> * Sets whether a &quot;credentialed&quot; request that is aware of cookie and
<a name="line420"></a> * authentication information should be made. This option is only supported by
<a name="line421"></a> * browsers that support HTTP Access Control. As of this writing, this option
<a name="line422"></a> * is not supported in IE.
<a name="line423"></a> *
<a name="line424"></a> * @param {boolean} withCredentials Whether this should be a &quot;credentialed&quot;
<a name="line425"></a> *     request.
<a name="line426"></a> */
<a name="line427"></a>goog.net.XhrIo.prototype.setWithCredentials = function(withCredentials) {
<a name="line428"></a>  this.withCredentials_ = withCredentials;
<a name="line429"></a>};
<a name="line430"></a>
<a name="line431"></a>
<a name="line432"></a>/**
<a name="line433"></a> * Gets whether a &quot;credentialed&quot; request is to be sent.
<a name="line434"></a> * @return {boolean} The desired type for the response.
<a name="line435"></a> */
<a name="line436"></a>goog.net.XhrIo.prototype.getWithCredentials = function() {
<a name="line437"></a>  return this.withCredentials_;
<a name="line438"></a>};
<a name="line439"></a>
<a name="line440"></a>
<a name="line441"></a>/**
<a name="line442"></a> * Instance send that actually uses XMLHttpRequest to make a server call.
<a name="line443"></a> * @param {string|goog.Uri} url Uri to make request to.
<a name="line444"></a> * @param {string=} opt_method Send method, default: GET.
<a name="line445"></a> * @param {ArrayBuffer|Blob|Document|FormData|GearsBlob|string=} opt_content
<a name="line446"></a> *     Post data. This can be a Gears blob if the underlying HTTP request object
<a name="line447"></a> *     is a Gears HTTP request.
<a name="line448"></a> * @param {Object|goog.structs.Map=} opt_headers Map of headers to add to the
<a name="line449"></a> *     request.
<a name="line450"></a> */
<a name="line451"></a>goog.net.XhrIo.prototype.send = function(url, opt_method, opt_content,
<a name="line452"></a>                                         opt_headers) {
<a name="line453"></a>  if (this.xhr_) {
<a name="line454"></a>    throw Error(&#39;[goog.net.XhrIo] Object is active with another request=&#39; +
<a name="line455"></a>        this.lastUri_ + &#39;; newUri=&#39; + url);
<a name="line456"></a>  }
<a name="line457"></a>
<a name="line458"></a>  var method = opt_method ? opt_method.toUpperCase() : &#39;GET&#39;;
<a name="line459"></a>
<a name="line460"></a>  this.lastUri_ = url;
<a name="line461"></a>  this.lastError_ = &#39;&#39;;
<a name="line462"></a>  this.lastErrorCode_ = goog.net.ErrorCode.NO_ERROR;
<a name="line463"></a>  this.lastMethod_ = method;
<a name="line464"></a>  this.errorDispatched_ = false;
<a name="line465"></a>  this.active_ = true;
<a name="line466"></a>
<a name="line467"></a>  // Use the factory to create the XHR object and options
<a name="line468"></a>  this.xhr_ = this.createXhr();
<a name="line469"></a>  this.xhrOptions_ = this.xmlHttpFactory_ ?
<a name="line470"></a>      this.xmlHttpFactory_.getOptions() : goog.net.XmlHttp.getOptions();
<a name="line471"></a>
<a name="line472"></a>  // Set up the onreadystatechange callback
<a name="line473"></a>  this.xhr_.onreadystatechange = goog.bind(this.onReadyStateChange_, this);
<a name="line474"></a>
<a name="line475"></a>  /**
<a name="line476"></a>   * Try to open the XMLHttpRequest (always async), if an error occurs here it
<a name="line477"></a>   * is generally permission denied
<a name="line478"></a>   * @preserveTry
<a name="line479"></a>   */
<a name="line480"></a>  try {
<a name="line481"></a>    this.logger_.fine(this.formatMsg_(&#39;Opening Xhr&#39;));
<a name="line482"></a>    this.inOpen_ = true;
<a name="line483"></a>    this.xhr_.open(method, url, true);  // Always async!
<a name="line484"></a>    this.inOpen_ = false;
<a name="line485"></a>  } catch (err) {
<a name="line486"></a>    this.logger_.fine(this.formatMsg_(&#39;Error opening Xhr: &#39; + err.message));
<a name="line487"></a>    this.error_(goog.net.ErrorCode.EXCEPTION, err);
<a name="line488"></a>    return;
<a name="line489"></a>  }
<a name="line490"></a>
<a name="line491"></a>  // We can&#39;t use null since this won&#39;t allow POSTs to have a content length
<a name="line492"></a>  // specified which will cause some proxies to return a 411 error.
<a name="line493"></a>  var content = opt_content || &#39;&#39;;
<a name="line494"></a>
<a name="line495"></a>  var headers = this.headers.clone();
<a name="line496"></a>
<a name="line497"></a>  // Add headers specific to this request
<a name="line498"></a>  if (opt_headers) {
<a name="line499"></a>    goog.structs.forEach(opt_headers, function(value, key) {
<a name="line500"></a>      headers.set(key, value);
<a name="line501"></a>    });
<a name="line502"></a>  }
<a name="line503"></a>
<a name="line504"></a>  var contentIsFormData = (goog.global[&#39;FormData&#39;] &amp;&amp;
<a name="line505"></a>      (content instanceof goog.global[&#39;FormData&#39;]));
<a name="line506"></a>  if (method == &#39;POST&#39; &amp;&amp;
<a name="line507"></a>      !headers.containsKey(goog.net.XhrIo.CONTENT_TYPE_HEADER) &amp;&amp;
<a name="line508"></a>      !contentIsFormData) {
<a name="line509"></a>    // For POST requests, default to the url-encoded form content type
<a name="line510"></a>    // unless this is a FormData request.  For FormData, the browser will
<a name="line511"></a>    // automatically add a multipart/form-data content type with an appropriate
<a name="line512"></a>    // multipart boundary.
<a name="line513"></a>    headers.set(goog.net.XhrIo.CONTENT_TYPE_HEADER,
<a name="line514"></a>                goog.net.XhrIo.FORM_CONTENT_TYPE);
<a name="line515"></a>  }
<a name="line516"></a>
<a name="line517"></a>  // Add the headers to the Xhr object
<a name="line518"></a>  goog.structs.forEach(headers, function(value, key) {
<a name="line519"></a>    this.xhr_.setRequestHeader(key, value);
<a name="line520"></a>  }, this);
<a name="line521"></a>
<a name="line522"></a>  if (this.responseType_) {
<a name="line523"></a>    this.xhr_.responseType = this.responseType_;
<a name="line524"></a>  }
<a name="line525"></a>
<a name="line526"></a>  if (goog.object.containsKey(this.xhr_, &#39;withCredentials&#39;)) {
<a name="line527"></a>    this.xhr_.withCredentials = this.withCredentials_;
<a name="line528"></a>  }
<a name="line529"></a>
<a name="line530"></a>  /**
<a name="line531"></a>   * Try to send the request, or other wise report an error (404 not found).
<a name="line532"></a>   * @preserveTry
<a name="line533"></a>   */
<a name="line534"></a>  try {
<a name="line535"></a>    if (this.timeoutId_) {
<a name="line536"></a>      // This should never happen, since the if (this.active_) above shouldn&#39;t
<a name="line537"></a>      // let execution reach this point if there is a request in progress...
<a name="line538"></a>      goog.Timer.defaultTimerObject.clearTimeout(this.timeoutId_);
<a name="line539"></a>      this.timeoutId_ = null;
<a name="line540"></a>    }
<a name="line541"></a>    if (this.timeoutInterval_ &gt; 0) {
<a name="line542"></a>      this.logger_.fine(this.formatMsg_(&#39;Will abort after &#39; +
<a name="line543"></a>          this.timeoutInterval_ + &#39;ms if incomplete&#39;));
<a name="line544"></a>      this.timeoutId_ = goog.Timer.defaultTimerObject.setTimeout(
<a name="line545"></a>          goog.bind(this.timeout_, this), this.timeoutInterval_);
<a name="line546"></a>    }
<a name="line547"></a>    this.logger_.fine(this.formatMsg_(&#39;Sending request&#39;));
<a name="line548"></a>    this.inSend_ = true;
<a name="line549"></a>    this.xhr_.send(content);
<a name="line550"></a>    this.inSend_ = false;
<a name="line551"></a>
<a name="line552"></a>  } catch (err) {
<a name="line553"></a>    this.logger_.fine(this.formatMsg_(&#39;Send error: &#39; + err.message));
<a name="line554"></a>    this.error_(goog.net.ErrorCode.EXCEPTION, err);
<a name="line555"></a>  }
<a name="line556"></a>};
<a name="line557"></a>
<a name="line558"></a>
<a name="line559"></a>/**
<a name="line560"></a> * Creates a new XHR object.
<a name="line561"></a> * @return {XMLHttpRequest|GearsHttpRequest} The newly created XHR object.
<a name="line562"></a> * @protected
<a name="line563"></a> */
<a name="line564"></a>goog.net.XhrIo.prototype.createXhr = function() {
<a name="line565"></a>  return this.xmlHttpFactory_ ?
<a name="line566"></a>      this.xmlHttpFactory_.createInstance() : goog.net.XmlHttp();
<a name="line567"></a>};
<a name="line568"></a>
<a name="line569"></a>
<a name="line570"></a>/**
<a name="line571"></a> * The request didn&#39;t complete after {@link goog.net.XhrIo#timeoutInterval_}
<a name="line572"></a> * milliseconds; raises a {@link goog.net.EventType.TIMEOUT} event and aborts
<a name="line573"></a> * the request.
<a name="line574"></a> * @private
<a name="line575"></a> */
<a name="line576"></a>goog.net.XhrIo.prototype.timeout_ = function() {
<a name="line577"></a>  if (typeof goog == &#39;undefined&#39;) {
<a name="line578"></a>    // If goog is undefined then the callback has occurred as the application
<a name="line579"></a>    // is unloading and will error.  Thus we let it silently fail.
<a name="line580"></a>  } else if (this.xhr_) {
<a name="line581"></a>    this.lastError_ = &#39;Timed out after &#39; + this.timeoutInterval_ +
<a name="line582"></a>                      &#39;ms, aborting&#39;;
<a name="line583"></a>    this.lastErrorCode_ = goog.net.ErrorCode.TIMEOUT;
<a name="line584"></a>    this.logger_.fine(this.formatMsg_(this.lastError_));
<a name="line585"></a>    this.dispatchEvent(goog.net.EventType.TIMEOUT);
<a name="line586"></a>    this.abort(goog.net.ErrorCode.TIMEOUT);
<a name="line587"></a>  }
<a name="line588"></a>};
<a name="line589"></a>
<a name="line590"></a>
<a name="line591"></a>/**
<a name="line592"></a> * Something errorred, so inactivate, fire error callback and clean up
<a name="line593"></a> * @param {goog.net.ErrorCode} errorCode The error code.
<a name="line594"></a> * @param {Error} err The error object.
<a name="line595"></a> * @private
<a name="line596"></a> */
<a name="line597"></a>goog.net.XhrIo.prototype.error_ = function(errorCode, err) {
<a name="line598"></a>  this.active_ = false;
<a name="line599"></a>  if (this.xhr_) {
<a name="line600"></a>    this.inAbort_ = true;
<a name="line601"></a>    this.xhr_.abort();  // Ensures XHR isn&#39;t hung (FF)
<a name="line602"></a>    this.inAbort_ = false;
<a name="line603"></a>  }
<a name="line604"></a>  this.lastError_ = err;
<a name="line605"></a>  this.lastErrorCode_ = errorCode;
<a name="line606"></a>  this.dispatchErrors_();
<a name="line607"></a>  this.cleanUpXhr_();
<a name="line608"></a>};
<a name="line609"></a>
<a name="line610"></a>
<a name="line611"></a>/**
<a name="line612"></a> * Dispatches COMPLETE and ERROR in case of an error. This ensures that we do
<a name="line613"></a> * not dispatch multiple error events.
<a name="line614"></a> * @private
<a name="line615"></a> */
<a name="line616"></a>goog.net.XhrIo.prototype.dispatchErrors_ = function() {
<a name="line617"></a>  if (!this.errorDispatched_) {
<a name="line618"></a>    this.errorDispatched_ = true;
<a name="line619"></a>    this.dispatchEvent(goog.net.EventType.COMPLETE);
<a name="line620"></a>    this.dispatchEvent(goog.net.EventType.ERROR);
<a name="line621"></a>  }
<a name="line622"></a>};
<a name="line623"></a>
<a name="line624"></a>
<a name="line625"></a>/**
<a name="line626"></a> * Abort the current XMLHttpRequest
<a name="line627"></a> * @param {goog.net.ErrorCode=} opt_failureCode Optional error code to use -
<a name="line628"></a> *     defaults to ABORT.
<a name="line629"></a> */
<a name="line630"></a>goog.net.XhrIo.prototype.abort = function(opt_failureCode) {
<a name="line631"></a>  if (this.xhr_ &amp;&amp; this.active_) {
<a name="line632"></a>    this.logger_.fine(this.formatMsg_(&#39;Aborting&#39;));
<a name="line633"></a>    this.active_ = false;
<a name="line634"></a>    this.inAbort_ = true;
<a name="line635"></a>    this.xhr_.abort();
<a name="line636"></a>    this.inAbort_ = false;
<a name="line637"></a>    this.lastErrorCode_ = opt_failureCode || goog.net.ErrorCode.ABORT;
<a name="line638"></a>    this.dispatchEvent(goog.net.EventType.COMPLETE);
<a name="line639"></a>    this.dispatchEvent(goog.net.EventType.ABORT);
<a name="line640"></a>    this.cleanUpXhr_();
<a name="line641"></a>  }
<a name="line642"></a>};
<a name="line643"></a>
<a name="line644"></a>
<a name="line645"></a>/**
<a name="line646"></a> * Nullifies all callbacks to reduce risks of leaks.
<a name="line647"></a> * @override
<a name="line648"></a> * @protected
<a name="line649"></a> */
<a name="line650"></a>goog.net.XhrIo.prototype.disposeInternal = function() {
<a name="line651"></a>  if (this.xhr_) {
<a name="line652"></a>    // We explicitly do not call xhr_.abort() unless active_ is still true.
<a name="line653"></a>    // This is to avoid unnecessarily aborting a successful request when
<a name="line654"></a>    // dispose() is called in a callback triggered by a complete response, but
<a name="line655"></a>    // in which browser cleanup has not yet finished.
<a name="line656"></a>    // (See http://b/issue?id=1684217.)
<a name="line657"></a>    if (this.active_) {
<a name="line658"></a>      this.active_ = false;
<a name="line659"></a>      this.inAbort_ = true;
<a name="line660"></a>      this.xhr_.abort();
<a name="line661"></a>      this.inAbort_ = false;
<a name="line662"></a>    }
<a name="line663"></a>    this.cleanUpXhr_(true);
<a name="line664"></a>  }
<a name="line665"></a>
<a name="line666"></a>  goog.net.XhrIo.superClass_.disposeInternal.call(this);
<a name="line667"></a>};
<a name="line668"></a>
<a name="line669"></a>
<a name="line670"></a>/**
<a name="line671"></a> * Internal handler for the XHR object&#39;s readystatechange event.  This method
<a name="line672"></a> * checks the status and the readystate and fires the correct callbacks.
<a name="line673"></a> * If the request has ended, the handlers are cleaned up and the XHR object is
<a name="line674"></a> * nullified.
<a name="line675"></a> * @private
<a name="line676"></a> */
<a name="line677"></a>goog.net.XhrIo.prototype.onReadyStateChange_ = function() {
<a name="line678"></a>  if (!this.inOpen_ &amp;&amp; !this.inSend_ &amp;&amp; !this.inAbort_) {
<a name="line679"></a>    // Were not being called from within a call to this.xhr_.send
<a name="line680"></a>    // this.xhr_.abort, or this.xhr_.open, so this is an entry point
<a name="line681"></a>    this.onReadyStateChangeEntryPoint_();
<a name="line682"></a>  } else {
<a name="line683"></a>    this.onReadyStateChangeHelper_();
<a name="line684"></a>  }
<a name="line685"></a>};
<a name="line686"></a>
<a name="line687"></a>
<a name="line688"></a>/**
<a name="line689"></a> * Used to protect the onreadystatechange handler entry point.  Necessary
<a name="line690"></a> * as {#onReadyStateChange_} maybe called from within send or abort, this
<a name="line691"></a> * method is only called when {#onReadyStateChange_} is called as an
<a name="line692"></a> * entry point.
<a name="line693"></a> * {@see #protectEntryPoints}
<a name="line694"></a> * @private
<a name="line695"></a> */
<a name="line696"></a>goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ = function() {
<a name="line697"></a>  this.onReadyStateChangeHelper_();
<a name="line698"></a>};
<a name="line699"></a>
<a name="line700"></a>
<a name="line701"></a>/**
<a name="line702"></a> * Helper for {@link #onReadyStateChange_}.  This is used so that
<a name="line703"></a> * entry point calls to {@link #onReadyStateChange_} can be routed through
<a name="line704"></a> * {@link #onReadyStateChangeEntryPoint_}.
<a name="line705"></a> * @private
<a name="line706"></a> */
<a name="line707"></a>goog.net.XhrIo.prototype.onReadyStateChangeHelper_ = function() {
<a name="line708"></a>  if (!this.active_) {
<a name="line709"></a>    // can get called inside abort call
<a name="line710"></a>    return;
<a name="line711"></a>  }
<a name="line712"></a>
<a name="line713"></a>  if (typeof goog == &#39;undefined&#39;) {
<a name="line714"></a>    // NOTE(user): If goog is undefined then the callback has occurred as the
<a name="line715"></a>    // application is unloading and will error.  Thus we let it silently fail.
<a name="line716"></a>
<a name="line717"></a>  } else if (
<a name="line718"></a>      this.xhrOptions_[goog.net.XmlHttp.OptionType.LOCAL_REQUEST_ERROR] &amp;&amp;
<a name="line719"></a>      this.getReadyState() == goog.net.XmlHttp.ReadyState.COMPLETE &amp;&amp;
<a name="line720"></a>      this.getStatus() == 2) {
<a name="line721"></a>    // NOTE(user): In IE if send() errors on a *local* request the readystate
<a name="line722"></a>    // is still changed to COMPLETE.  We need to ignore it and allow the
<a name="line723"></a>    // try/catch around send() to pick up the error.
<a name="line724"></a>    this.logger_.fine(this.formatMsg_(
<a name="line725"></a>        &#39;Local request error detected and ignored&#39;));
<a name="line726"></a>
<a name="line727"></a>  } else {
<a name="line728"></a>
<a name="line729"></a>    // In IE when the response has been cached we sometimes get the callback
<a name="line730"></a>    // from inside the send call and this usually breaks code that assumes that
<a name="line731"></a>    // XhrIo is asynchronous.  If that is the case we delay the callback
<a name="line732"></a>    // using a timer.
<a name="line733"></a>    if (this.inSend_ &amp;&amp;
<a name="line734"></a>        this.getReadyState() == goog.net.XmlHttp.ReadyState.COMPLETE) {
<a name="line735"></a>      goog.Timer.defaultTimerObject.setTimeout(
<a name="line736"></a>          goog.bind(this.onReadyStateChange_, this), 0);
<a name="line737"></a>      return;
<a name="line738"></a>    }
<a name="line739"></a>
<a name="line740"></a>    this.dispatchEvent(goog.net.EventType.READY_STATE_CHANGE);
<a name="line741"></a>
<a name="line742"></a>    // readyState indicates the transfer has finished
<a name="line743"></a>    if (this.isComplete()) {
<a name="line744"></a>      this.logger_.fine(this.formatMsg_(&#39;Request complete&#39;));
<a name="line745"></a>
<a name="line746"></a>      this.active_ = false;
<a name="line747"></a>
<a name="line748"></a>      try {
<a name="line749"></a>        // Call the specific callbacks for success or failure. Only call the
<a name="line750"></a>        // success if the status is 200 (HTTP_OK) or 304 (HTTP_CACHED)
<a name="line751"></a>        if (this.isSuccess()) {
<a name="line752"></a>          this.dispatchEvent(goog.net.EventType.COMPLETE);
<a name="line753"></a>          this.dispatchEvent(goog.net.EventType.SUCCESS);
<a name="line754"></a>        } else {
<a name="line755"></a>          this.lastErrorCode_ = goog.net.ErrorCode.HTTP_ERROR;
<a name="line756"></a>          this.lastError_ =
<a name="line757"></a>              this.getStatusText() + &#39; [&#39; + this.getStatus() + &#39;]&#39;;
<a name="line758"></a>          this.dispatchErrors_();
<a name="line759"></a>        }
<a name="line760"></a>      } finally {
<a name="line761"></a>        this.cleanUpXhr_();
<a name="line762"></a>      }
<a name="line763"></a>    }
<a name="line764"></a>  }
<a name="line765"></a>};
<a name="line766"></a>
<a name="line767"></a>
<a name="line768"></a>/**
<a name="line769"></a> * Remove the listener to protect against leaks, and nullify the XMLHttpRequest
<a name="line770"></a> * object.
<a name="line771"></a> * @param {boolean=} opt_fromDispose If this is from the dispose (don&#39;t want to
<a name="line772"></a> *     fire any events).
<a name="line773"></a> * @private
<a name="line774"></a> */
<a name="line775"></a>goog.net.XhrIo.prototype.cleanUpXhr_ = function(opt_fromDispose) {
<a name="line776"></a>  if (this.xhr_) {
<a name="line777"></a>    // Save reference so we can mark it as closed after the READY event.  The
<a name="line778"></a>    // READY event may trigger another request, thus we must nullify this.xhr_
<a name="line779"></a>    var xhr = this.xhr_;
<a name="line780"></a>    var clearedOnReadyStateChange =
<a name="line781"></a>        this.xhrOptions_[goog.net.XmlHttp.OptionType.USE_NULL_FUNCTION] ?
<a name="line782"></a>            goog.nullFunction : null;
<a name="line783"></a>    this.xhr_ = null;
<a name="line784"></a>    this.xhrOptions_ = null;
<a name="line785"></a>
<a name="line786"></a>    if (this.timeoutId_) {
<a name="line787"></a>      // Cancel any pending timeout event handler.
<a name="line788"></a>      goog.Timer.defaultTimerObject.clearTimeout(this.timeoutId_);
<a name="line789"></a>      this.timeoutId_ = null;
<a name="line790"></a>    }
<a name="line791"></a>
<a name="line792"></a>    if (!opt_fromDispose) {
<a name="line793"></a>      this.dispatchEvent(goog.net.EventType.READY);
<a name="line794"></a>    }
<a name="line795"></a>
<a name="line796"></a>    try {
<a name="line797"></a>      // NOTE(user): Not nullifying in FireFox can still leak if the callbacks
<a name="line798"></a>      // are defined in the same scope as the instance of XhrIo. But, IE doesn&#39;t
<a name="line799"></a>      // allow you to set the onreadystatechange to NULL so nullFunction is
<a name="line800"></a>      // used.
<a name="line801"></a>      xhr.onreadystatechange = clearedOnReadyStateChange;
<a name="line802"></a>    } catch (e) {
<a name="line803"></a>      // This seems to occur with a Gears HTTP request. Delayed the setting of
<a name="line804"></a>      // this onreadystatechange until after READY is sent out and catching the
<a name="line805"></a>      // error to see if we can track down the problem.
<a name="line806"></a>      this.logger_.severe(&#39;Problem encountered resetting onreadystatechange: &#39; +
<a name="line807"></a>                          e.message);
<a name="line808"></a>    }
<a name="line809"></a>  }
<a name="line810"></a>};
<a name="line811"></a>
<a name="line812"></a>
<a name="line813"></a>/**
<a name="line814"></a> * @return {boolean} Whether there is an active request.
<a name="line815"></a> */
<a name="line816"></a>goog.net.XhrIo.prototype.isActive = function() {
<a name="line817"></a>  return !!this.xhr_;
<a name="line818"></a>};
<a name="line819"></a>
<a name="line820"></a>
<a name="line821"></a>/**
<a name="line822"></a> * @return {boolean} Whether the request has completed.
<a name="line823"></a> */
<a name="line824"></a>goog.net.XhrIo.prototype.isComplete = function() {
<a name="line825"></a>  return this.getReadyState() == goog.net.XmlHttp.ReadyState.COMPLETE;
<a name="line826"></a>};
<a name="line827"></a>
<a name="line828"></a>
<a name="line829"></a>/**
<a name="line830"></a> * @return {boolean} Whether the request completed with a success.
<a name="line831"></a> */
<a name="line832"></a>goog.net.XhrIo.prototype.isSuccess = function() {
<a name="line833"></a>  var status = this.getStatus();
<a name="line834"></a>  // A zero status code is considered successful for local files.
<a name="line835"></a>  return goog.net.HttpStatus.isSuccess(status) ||
<a name="line836"></a>      status === 0 &amp;&amp; !this.isLastUriEffectiveSchemeHttp_();
<a name="line837"></a>};
<a name="line838"></a>
<a name="line839"></a>
<a name="line840"></a>/**
<a name="line841"></a> * @return {boolean} whether the effective scheme of the last URI that was
<a name="line842"></a> *     fetched was &#39;http&#39; or &#39;https&#39;.
<a name="line843"></a> * @private
<a name="line844"></a> */
<a name="line845"></a>goog.net.XhrIo.prototype.isLastUriEffectiveSchemeHttp_ = function() {
<a name="line846"></a>  var scheme = goog.uri.utils.getEffectiveScheme(String(this.lastUri_));
<a name="line847"></a>  return goog.net.XhrIo.HTTP_SCHEME_PATTERN.test(scheme);
<a name="line848"></a>};
<a name="line849"></a>
<a name="line850"></a>
<a name="line851"></a>/**
<a name="line852"></a> * Get the readystate from the Xhr object
<a name="line853"></a> * Will only return correct result when called from the context of a callback
<a name="line854"></a> * @return {goog.net.XmlHttp.ReadyState} goog.net.XmlHttp.ReadyState.*.
<a name="line855"></a> */
<a name="line856"></a>goog.net.XhrIo.prototype.getReadyState = function() {
<a name="line857"></a>  return this.xhr_ ?
<a name="line858"></a>      /** @type {goog.net.XmlHttp.ReadyState} */ (this.xhr_.readyState) :
<a name="line859"></a>      goog.net.XmlHttp.ReadyState.UNINITIALIZED;
<a name="line860"></a>};
<a name="line861"></a>
<a name="line862"></a>
<a name="line863"></a>/**
<a name="line864"></a> * Get the status from the Xhr object
<a name="line865"></a> * Will only return correct result when called from the context of a callback
<a name="line866"></a> * @return {number} Http status.
<a name="line867"></a> */
<a name="line868"></a>goog.net.XhrIo.prototype.getStatus = function() {
<a name="line869"></a>  /**
<a name="line870"></a>   * IE doesn&#39;t like you checking status until the readystate is greater than 2
<a name="line871"></a>   * (i.e. it is recieving or complete).  The try/catch is used for when the
<a name="line872"></a>   * page is unloading and an ERROR_NOT_AVAILABLE may occur when accessing xhr_.
<a name="line873"></a>   * @preserveTry
<a name="line874"></a>   */
<a name="line875"></a>  try {
<a name="line876"></a>    return this.getReadyState() &gt; goog.net.XmlHttp.ReadyState.LOADED ?
<a name="line877"></a>        this.xhr_.status : -1;
<a name="line878"></a>  } catch (e) {
<a name="line879"></a>    this.logger_.warning(&#39;Can not get status: &#39; + e.message);
<a name="line880"></a>    return -1;
<a name="line881"></a>  }
<a name="line882"></a>};
<a name="line883"></a>
<a name="line884"></a>
<a name="line885"></a>/**
<a name="line886"></a> * Get the status text from the Xhr object
<a name="line887"></a> * Will only return correct result when called from the context of a callback
<a name="line888"></a> * @return {string} Status text.
<a name="line889"></a> */
<a name="line890"></a>goog.net.XhrIo.prototype.getStatusText = function() {
<a name="line891"></a>  /**
<a name="line892"></a>   * IE doesn&#39;t like you checking status until the readystate is greater than 2
<a name="line893"></a>   * (i.e. it is recieving or complete).  The try/catch is used for when the
<a name="line894"></a>   * page is unloading and an ERROR_NOT_AVAILABLE may occur when accessing xhr_.
<a name="line895"></a>   * @preserveTry
<a name="line896"></a>   */
<a name="line897"></a>  try {
<a name="line898"></a>    return this.getReadyState() &gt; goog.net.XmlHttp.ReadyState.LOADED ?
<a name="line899"></a>        this.xhr_.statusText : &#39;&#39;;
<a name="line900"></a>  } catch (e) {
<a name="line901"></a>    this.logger_.fine(&#39;Can not get status: &#39; + e.message);
<a name="line902"></a>    return &#39;&#39;;
<a name="line903"></a>  }
<a name="line904"></a>};
<a name="line905"></a>
<a name="line906"></a>
<a name="line907"></a>/**
<a name="line908"></a> * Get the last Uri that was requested
<a name="line909"></a> * @return {string} Last Uri.
<a name="line910"></a> */
<a name="line911"></a>goog.net.XhrIo.prototype.getLastUri = function() {
<a name="line912"></a>  return String(this.lastUri_);
<a name="line913"></a>};
<a name="line914"></a>
<a name="line915"></a>
<a name="line916"></a>/**
<a name="line917"></a> * Get the response text from the Xhr object
<a name="line918"></a> * Will only return correct result when called from the context of a callback.
<a name="line919"></a> * @return {string} Result from the server, or &#39;&#39; if no result available.
<a name="line920"></a> */
<a name="line921"></a>goog.net.XhrIo.prototype.getResponseText = function() {
<a name="line922"></a>  /** @preserveTry */
<a name="line923"></a>  try {
<a name="line924"></a>    return this.xhr_ ? this.xhr_.responseText : &#39;&#39;;
<a name="line925"></a>  } catch (e) {
<a name="line926"></a>    // http://www.w3.org/TR/XMLHttpRequest/#the-responsetext-attribute
<a name="line927"></a>    // states that responseText should return &#39;&#39; (and responseXML null)
<a name="line928"></a>    // when the state is not LOADING or DONE. Instead, IE and Gears can
<a name="line929"></a>    // throw unexpected exceptions, for example when a request is aborted
<a name="line930"></a>    // or no data is available yet.
<a name="line931"></a>    this.logger_.fine(&#39;Can not get responseText: &#39; + e.message);
<a name="line932"></a>    return &#39;&#39;;
<a name="line933"></a>  }
<a name="line934"></a>};
<a name="line935"></a>
<a name="line936"></a>
<a name="line937"></a>/**
<a name="line938"></a> * Get the response body from the Xhr object. This property is only available
<a name="line939"></a> * in IE since version 7 according to MSDN:
<a name="line940"></a> * http://msdn.microsoft.com/en-us/library/ie/ms534368(v=vs.85).aspx
<a name="line941"></a> * Will only return correct result when called from the context of a callback.
<a name="line942"></a> *
<a name="line943"></a> * One option is to construct a VBArray from the returned object and convert
<a name="line944"></a> * it to a JavaScript array using the toArray method:
<a name="line945"></a> * {@code (new window[&#39;VBArray&#39;](xhrIo.getResponseBody())).toArray()}
<a name="line946"></a> * This will result in an array of numbers in the range of [0..255]
<a name="line947"></a> *
<a name="line948"></a> * Another option is to use the VBScript CStr method to convert it into a
<a name="line949"></a> * string as outlined in http://stackoverflow.com/questions/1919972
<a name="line950"></a> *
<a name="line951"></a> * @return {Object} Binary result from the server or null if not available.
<a name="line952"></a> */
<a name="line953"></a>goog.net.XhrIo.prototype.getResponseBody = function() {
<a name="line954"></a>  /** @preserveTry */
<a name="line955"></a>  try {
<a name="line956"></a>    if (this.xhr_ &amp;&amp; &#39;responseBody&#39; in this.xhr_) {
<a name="line957"></a>      return this.xhr_[&#39;responseBody&#39;];
<a name="line958"></a>    }
<a name="line959"></a>  } catch (e) {
<a name="line960"></a>    // IE can throw unexpected exceptions, for example when a request is aborted
<a name="line961"></a>    // or no data is yet available.
<a name="line962"></a>    this.logger_.fine(&#39;Can not get responseBody: &#39; + e.message);
<a name="line963"></a>  }
<a name="line964"></a>  return null;
<a name="line965"></a>};
<a name="line966"></a>
<a name="line967"></a>
<a name="line968"></a>/**
<a name="line969"></a> * Get the response XML from the Xhr object
<a name="line970"></a> * Will only return correct result when called from the context of a callback.
<a name="line971"></a> * @return {Document} The DOM Document representing the XML file, or null
<a name="line972"></a> * if no result available.
<a name="line973"></a> */
<a name="line974"></a>goog.net.XhrIo.prototype.getResponseXml = function() {
<a name="line975"></a>  /** @preserveTry */
<a name="line976"></a>  try {
<a name="line977"></a>    return this.xhr_ ? this.xhr_.responseXML : null;
<a name="line978"></a>  } catch (e) {
<a name="line979"></a>    this.logger_.fine(&#39;Can not get responseXML: &#39; + e.message);
<a name="line980"></a>    return null;
<a name="line981"></a>  }
<a name="line982"></a>};
<a name="line983"></a>
<a name="line984"></a>
<a name="line985"></a>/**
<a name="line986"></a> * Get the response and evaluates it as JSON from the Xhr object
<a name="line987"></a> * Will only return correct result when called from the context of a callback
<a name="line988"></a> * @param {string=} opt_xssiPrefix Optional XSSI prefix string to use for
<a name="line989"></a> *     stripping of the response before parsing. This needs to be set only if
<a name="line990"></a> *     your backend server prepends the same prefix string to the JSON response.
<a name="line991"></a> * @return {Object|undefined} JavaScript object.
<a name="line992"></a> */
<a name="line993"></a>goog.net.XhrIo.prototype.getResponseJson = function(opt_xssiPrefix) {
<a name="line994"></a>  if (!this.xhr_) {
<a name="line995"></a>    return undefined;
<a name="line996"></a>  }
<a name="line997"></a>
<a name="line998"></a>  var responseText = this.xhr_.responseText;
<a name="line999"></a>  if (opt_xssiPrefix &amp;&amp; responseText.indexOf(opt_xssiPrefix) == 0) {
<a name="line1000"></a>    responseText = responseText.substring(opt_xssiPrefix.length);
<a name="line1001"></a>  }
<a name="line1002"></a>
<a name="line1003"></a>  return goog.json.parse(responseText);
<a name="line1004"></a>};
<a name="line1005"></a>
<a name="line1006"></a>
<a name="line1007"></a>/**
<a name="line1008"></a> * Get the response as the type specificed by {@link #setResponseType}. At time
<a name="line1009"></a> * of writing, this is only directly supported in very recent versions of WebKit
<a name="line1010"></a> * (10.0.612.1 dev and later). If the field is not supported directly, we will
<a name="line1011"></a> * try to emulate it.
<a name="line1012"></a> *
<a name="line1013"></a> * Emulating the response means following the rules laid out at
<a name="line1014"></a> * http://www.w3.org/TR/XMLHttpRequest/#the-response-attribute
<a name="line1015"></a> *
<a name="line1016"></a> * On browsers with no support for this (Chrome &lt; 10, Firefox &lt; 4, etc), only
<a name="line1017"></a> * response types of DEFAULT or TEXT may be used, and the response returned will
<a name="line1018"></a> * be the text response.
<a name="line1019"></a> *
<a name="line1020"></a> * On browsers with Mozilla&#39;s draft support for array buffers (Firefox 4, 5),
<a name="line1021"></a> * only response types of DEFAULT, TEXT, and ARRAY_BUFFER may be used, and the
<a name="line1022"></a> * response returned will be either the text response or the Mozilla
<a name="line1023"></a> * implementation of the array buffer response.
<a name="line1024"></a> *
<a name="line1025"></a> * On browsers will full support, any valid response type supported by the
<a name="line1026"></a> * browser may be used, and the response provided by the browser will be
<a name="line1027"></a> * returned.
<a name="line1028"></a> *
<a name="line1029"></a> * @return {*} The response.
<a name="line1030"></a> */
<a name="line1031"></a>goog.net.XhrIo.prototype.getResponse = function() {
<a name="line1032"></a>  /** @preserveTry */
<a name="line1033"></a>  try {
<a name="line1034"></a>    if (!this.xhr_) {
<a name="line1035"></a>      return null;
<a name="line1036"></a>    }
<a name="line1037"></a>    if (&#39;response&#39; in this.xhr_) {
<a name="line1038"></a>      return this.xhr_.response;
<a name="line1039"></a>    }
<a name="line1040"></a>    switch (this.responseType_) {
<a name="line1041"></a>      case goog.net.XhrIo.ResponseType.DEFAULT:
<a name="line1042"></a>      case goog.net.XhrIo.ResponseType.TEXT:
<a name="line1043"></a>        return this.xhr_.responseText;
<a name="line1044"></a>        // DOCUMENT and BLOB don&#39;t need to be handled here because they are
<a name="line1045"></a>        // introduced in the same spec that adds the .response field, and would
<a name="line1046"></a>        // have been caught above.
<a name="line1047"></a>        // ARRAY_BUFFER needs an implementation for Firefox 4, where it was
<a name="line1048"></a>        // implemented using a draft spec rather than the final spec.
<a name="line1049"></a>      case goog.net.XhrIo.ResponseType.ARRAY_BUFFER:
<a name="line1050"></a>        if (&#39;mozResponseArrayBuffer&#39; in this.xhr_) {
<a name="line1051"></a>          return this.xhr_.mozResponseArrayBuffer;
<a name="line1052"></a>        }
<a name="line1053"></a>    }
<a name="line1054"></a>    // Fell through to a response type that is not supported on this browser.
<a name="line1055"></a>    this.logger_.severe(&#39;Response type &#39; + this.responseType_ + &#39; is not &#39; +
<a name="line1056"></a>                        &#39;supported on this browser&#39;);
<a name="line1057"></a>    return null;
<a name="line1058"></a>  } catch (e) {
<a name="line1059"></a>    this.logger_.fine(&#39;Can not get response: &#39; + e.message);
<a name="line1060"></a>    return null;
<a name="line1061"></a>  }
<a name="line1062"></a>};
<a name="line1063"></a>
<a name="line1064"></a>
<a name="line1065"></a>/**
<a name="line1066"></a> * Get the value of the response-header with the given name from the Xhr object
<a name="line1067"></a> * Will only return correct result when called from the context of a callback
<a name="line1068"></a> * and the request has completed
<a name="line1069"></a> * @param {string} key The name of the response-header to retrieve.
<a name="line1070"></a> * @return {string|undefined} The value of the response-header named key.
<a name="line1071"></a> */
<a name="line1072"></a>goog.net.XhrIo.prototype.getResponseHeader = function(key) {
<a name="line1073"></a>  return this.xhr_ &amp;&amp; this.isComplete() ?
<a name="line1074"></a>      this.xhr_.getResponseHeader(key) : undefined;
<a name="line1075"></a>};
<a name="line1076"></a>
<a name="line1077"></a>
<a name="line1078"></a>/**
<a name="line1079"></a> * Gets the text of all the headers in the response.
<a name="line1080"></a> * Will only return correct result when called from the context of a callback
<a name="line1081"></a> * and the request has completed.
<a name="line1082"></a> * @return {string} The value of the response headers or empty string.
<a name="line1083"></a> */
<a name="line1084"></a>goog.net.XhrIo.prototype.getAllResponseHeaders = function() {
<a name="line1085"></a>  return this.xhr_ &amp;&amp; this.isComplete() ?
<a name="line1086"></a>      this.xhr_.getAllResponseHeaders() : &#39;&#39;;
<a name="line1087"></a>};
<a name="line1088"></a>
<a name="line1089"></a>
<a name="line1090"></a>/**
<a name="line1091"></a> * Get the last error message
<a name="line1092"></a> * @return {goog.net.ErrorCode} Last error code.
<a name="line1093"></a> */
<a name="line1094"></a>goog.net.XhrIo.prototype.getLastErrorCode = function() {
<a name="line1095"></a>  return this.lastErrorCode_;
<a name="line1096"></a>};
<a name="line1097"></a>
<a name="line1098"></a>
<a name="line1099"></a>/**
<a name="line1100"></a> * Get the last error message
<a name="line1101"></a> * @return {string} Last error message.
<a name="line1102"></a> */
<a name="line1103"></a>goog.net.XhrIo.prototype.getLastError = function() {
<a name="line1104"></a>  return goog.isString(this.lastError_) ? this.lastError_ :
<a name="line1105"></a>      String(this.lastError_);
<a name="line1106"></a>};
<a name="line1107"></a>
<a name="line1108"></a>
<a name="line1109"></a>/**
<a name="line1110"></a> * Adds the last method, status and URI to the message.  This is used to add
<a name="line1111"></a> * this information to the logging calls.
<a name="line1112"></a> * @param {string} msg The message text that we want to add the extra text to.
<a name="line1113"></a> * @return {string} The message with the extra text appended.
<a name="line1114"></a> * @private
<a name="line1115"></a> */
<a name="line1116"></a>goog.net.XhrIo.prototype.formatMsg_ = function(msg) {
<a name="line1117"></a>  return msg + &#39; [&#39; + this.lastMethod_ + &#39; &#39; + this.lastUri_ + &#39; &#39; +
<a name="line1118"></a>      this.getStatus() + &#39;]&#39;;
<a name="line1119"></a>};
<a name="line1120"></a>
<a name="line1121"></a>
<a name="line1122"></a>// Register the xhr handler as an entry point, so that
<a name="line1123"></a>// it can be monitored for exception handling, etc.
<a name="line1124"></a>goog.debug.entryPointRegistry.register(
<a name="line1125"></a>    /**
<a name="line1126"></a>     * @param {function(!Function): !Function} transformer The transforming
<a name="line1127"></a>     *     function.
<a name="line1128"></a>     */
<a name="line1129"></a>    function(transformer) {
<a name="line1130"></a>      goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_ =
<a name="line1131"></a>          transformer(goog.net.XhrIo.prototype.onReadyStateChangeEntryPoint_);
<a name="line1132"></a>    });
</pre>


</body>
</html>
